package com.etc.ssm.service.impl;

import com.etc.ssm.dao.UserMapper;
import com.etc.ssm.entity.User;
import com.etc.ssm.entity.UserExample;
import com.etc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询单个用户信息
     * @param userid
     * @return
     */
    @Override
    @Cacheable(key = "#userid",cacheNames = "getusersbyid")
    public User getUserById(int userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    /**
     * 修改用户信息 ,CacheEvict 清除缓存中对应的记录
     * @param user
     * @return
     */
    @Override
    @CacheEvict(key = "#user.userid",cacheNames = "getusersbyid")
    @CachePut
    public int updateUser(User user){
        return userMapper.updateByPrimaryKey(user);
    }

    @Override
    public List<User> getUsers() {
        UserExample userExample = new UserExample();
        //userExample.createCriteria().andUseridEqualTo(1);
        return  userMapper.selectByExample(userExample);
    }
}
